package com.mm.dss.demo;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.github.library.BaseRecyclerAdapter;
import com.github.library.BaseViewHolder;
import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mm.dss.demo.Interface.OkGoUtilsInterFace;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.bean.QfQcBean;
import com.mm.dss.demo.bean.XxCjBean;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.OkgoUtils;
import com.mm.dss.demo.tools.SharedPreferencedUtils;
import com.mm.dss.demo.tools.SwipeRefreshHelper;
import com.mm.dss.demo.tools.WebServiceUtils;
import com.mm.dss.demo.tools.WeiboDialogUtils;
import com.mm.dss.demo.view.LogoutDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class QfqcActivity extends BaseActivity {
    RecyclerView recyclerView;
    RelativeLayout layout_back;
    TextView txt_title;

    private Dialog mWeiboDialog;
    List<QfQcBean> qfqcBeanlist = new ArrayList<>();
    BaseRecyclerAdapter<QfQcBean> baseRecyclerAdapter;
    private SwipeRefreshLayout swipregresh;//


    private void setnet(boolean IsDialog) {
        if (qfqcBeanlist.size() > 0) {
            qfqcBeanlist.clear();
        }
        if (IsDialog) {
            mWeiboDialog = WeiboDialogUtils.
                    createLoadingDialog(QfqcActivity.this,
                            "加载中...");
        }
        Map<String, String> map = new HashMap<>();
        map.put("role", SharedPreferencedUtils.getString(QfqcActivity.this, "Level"));
        WebServiceUtils.callWebService(Constances.webhosturl, "N05_GetAllQCQF", map, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                if (IsDialog) {
                    WeiboDialogUtils.closeDialog(mWeiboDialog);
                }
                finishRefresh();
                try {
                    JSONArray array = new JSONArray(result.getProperty(0).toString());
                    for (int i = 0; i < array.length(); i++) {
                        Gson gson = new Gson();
                        QfQcBean qfqcBean = gson.fromJson(array.optJSONObject(i).toString(), QfQcBean.class);
                        qfqcBeanlist.add(qfqcBean);
                    }
                    baseRecyclerAdapter.openLoadAnimation(false);
                    baseRecyclerAdapter.setData(qfqcBeanlist);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qfqc);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        layout_back = (RelativeLayout) findViewById(R.id.layout_back);
        txt_title = (TextView) findViewById(R.id.txt_title);
        swipregresh = (SwipeRefreshLayout) findViewById(R.id.swipregresh);
        initdata();
        initSwipeRefresh();
    }

    protected void initdata() {
        txt_title.setText("群防群测任务");
        layout_back.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        baseRecyclerAdapter = new BaseRecyclerAdapter<QfQcBean>
                (this, null, R.layout.item_qfqcview) {
            @Override
            protected void convert(final BaseViewHolder helper, final QfQcBean item) {
                helper.setText(R.id.title, item.getKSMC());

                final TextView isw = helper.getConvertView().findViewById(R.id.isw);
                final TextView time = helper.getConvertView().findViewById(R.id.time);
                switch (Integer.parseInt(item.getZT())) {
                    case 0:
                        isw.setText("未接受");
                        helper.setText(R.id.xfr, "下发人：" + item.getXCRY());
                        time.setText(item.getXCRQ());
                        break;
                    case 1:
                        isw.setText("未完成");
                        helper.setText(R.id.xfr, "接收人：" + item.getXFRY());
                        time.setText(item.getXCRQ());
                        break;
                    case 2:
                        isw.setText("已完成");
                        helper.setText(R.id.xfr, "接收人：" + item.getXFRY());
                        time.setText(item.getXCRQ());//完成时间
                        break;
                    case 3:
                        isw.setText("未下发");
                        helper.setText(R.id.xfr, "采集人：" + item.getXCRY());
                        time.setText(item.getXCRQ());
                        break;
                }
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(QfqcActivity.this, QfqcInfoActivity.class);
                        intent.putExtra("rwid", item.getID());  //任务ID
                        startActivity(intent);
                    }
                });

            }
        };
        baseRecyclerAdapter.openLoadAnimation(false);
        recyclerView.setAdapter(baseRecyclerAdapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        setnet(true);
    }

    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.layout_back://返回按钮
                finish();
                break;
        }
    }

    /**
     * 初始化下拉刷新
     */
    private void initSwipeRefresh() {
        if (swipregresh != null) {
            SwipeRefreshHelper.init(swipregresh, new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    setnet(false);

                }
            });
        }
    }

    /**
     * 关闭刷新
     */
    public void finishRefresh() {
        if (swipregresh != null) {
            swipregresh.setRefreshing(false);
        }
    }
}
